package com.lintf.devdemo.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lintf.devdemo.dao.UserAccountMapper;
import com.lintf.devdemo.dao.UserInfoMapper;
import com.lintf.devdemo.entity.dto.UserInfoDeleteDto;
import com.lintf.devdemo.entity.dto.UserInfoInsertDto;
import com.lintf.devdemo.entity.dto.UserInfoSelectDto;
import com.lintf.devdemo.entity.dto.UserInfoUpdateDto;
import com.lintf.devdemo.entity.po.UserAccount;
import com.lintf.devdemo.entity.po.UserInfo;
import com.lintf.devdemo.entity.vo.UserInfoVo;
import com.lintf.devdemo.service.UserService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
 * @ system:Window
 * @ author:lin tingfeng
 * @ data:2022/09/29-11:28
 * @ package_name:com.lintf.devdemo.service.Impl
 * @ project_name:DevDemo
 * @ desc:用户业务逻辑实现
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserService {

    @Autowired
    private UserAccountMapper userAccountMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public Integer insertUserInfo(@NotNull UserInfoInsertDto userInfoInsertDto) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("account", userInfoInsertDto.getName());
        List selectList = userAccountMapper.selectList(queryWrapper);
        if (selectList.size() == 0) {
            UserAccount userAccount = new UserAccount(userInfoInsertDto.getName(), userInfoInsertDto.getPassword());
            if (userAccountMapper.insert(userAccount) != 0) {
                UserAccount selectOne = userAccountMapper.selectOne(queryWrapper);
                UserInfo userInfo = new UserInfo(
                        selectOne.getUserId(),
                        userInfoInsertDto.getName(),
                        userInfoInsertDto.getAge(),
                        userInfoInsertDto.getEmail(),
                        userInfoInsertDto.getCreatorId(),
                        LocalDateTime.now(),
                        null,
                        LocalDateTime.now());
                return userInfoMapper.insert(userInfo);
            }
        }
        return 0;
    }

    @Override
    public Integer updateUserInfo(@NotNull UserInfoUpdateDto userInfoUpdateDto) {
        UserInfoVo userInfoVo = userInfoMapper.getUserInfoListById(userInfoUpdateDto.getId());
        Integer updateDtoAge = userInfoUpdateDto.getAge();
        String updateDtoEmail = userInfoUpdateDto.getEmail();
        String updateDtoPassword = userInfoUpdateDto.getPassword();
        if (updateDtoAge != null || updateDtoAge != 0)
            userInfoVo.setAge(updateDtoAge);
        else
            userInfoVo.setAge(userInfoVo.getAge());
        if (updateDtoEmail == null || updateDtoEmail.equals(""))
            userInfoVo.setEmail(userInfoVo.getEmail());
        else
            userInfoVo.setEmail(updateDtoEmail);
        if (updateDtoPassword == null || updateDtoPassword.equals(""))
            userInfoVo.setPassword(userInfoVo.getPassword());
        else
            userInfoVo.setPassword(updateDtoPassword);
        String jsonString = JSONObject.toJSONString(userInfoVo);
        UserAccount userAccount = JSONObject.parseObject(jsonString, UserAccount.class);
        userAccount.setUserId(userInfoVo.getId());
        userAccount.setUpdaterId(userInfoUpdateDto.getUpdaterId());
        userAccount.setUpdateTime(LocalDateTime.now());
        UserInfo userInfo = JSONObject.parseObject(jsonString, UserInfo.class);
        userInfo.setUpdaterId(userInfoUpdateDto.getUpdaterId());
        userInfo.setUpdateTime(LocalDateTime.now());
        if (userAccountMapper.updateById(userAccount) != 0 && userInfoMapper.updateById(userInfo) != 0)
            return 1;
        return 0;
    }

    @Override
    public Integer deleteUserInfo(@NotNull UserInfoDeleteDto userInfoDeleteDto) {
        Integer deleteDtoId = userInfoDeleteDto.getId();
        String deleteDtoAccount = userInfoDeleteDto.getAccount();
        if (deleteDtoId == null || deleteDtoId == 0) {
            if (deleteDtoAccount == null || deleteDtoAccount.equals("")) {
                return 0;
            } else { // 只传输account参数
                QueryWrapper queryUserAccountWrapper = new QueryWrapper();
                queryUserAccountWrapper.eq("account", deleteDtoAccount);
                UserAccount userAccount = userAccountMapper.selectOne(queryUserAccountWrapper);
                if (userAccount == null) return 0;
                QueryWrapper queryUserInfoWrapper = new QueryWrapper();
                queryUserInfoWrapper.eq("id", userAccount.getUserId());
                if (userInfoMapper.delete(queryUserInfoWrapper) != 0 && userAccountMapper.delete(queryUserAccountWrapper) != 0)
                    return 1;
            }
        } else { // 只传输id或传输id和account
            QueryWrapper queryUserInfoWrapper = new QueryWrapper();
            queryUserInfoWrapper.eq("id", deleteDtoId);
            UserAccount userAccount = userAccountMapper.selectById(deleteDtoId);
            if (userAccount == null) return 2;
            if (userAccount.getAccount().equals(deleteDtoAccount) || deleteDtoAccount == null) {
                if (userInfoMapper.delete(queryUserInfoWrapper) != 0 && userAccountMapper.deleteById(deleteDtoId) != 0)
                    return 1;
            } else return 2;
        }
        return 0;
    }

    @Override
    public Integer deleteUserInfoById(Integer userInfoId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", userInfoId);
        if (userAccountMapper.deleteById(userInfoId) != 0 && userInfoMapper.delete(queryWrapper) != 0) {
            return 1;
        }
        return 0;

    }

    @Override
    public List<UserInfoVo> getUserInfoList() {
        return userInfoMapper.getUserInfoList();
    }

    @Override
    public List<UserInfoVo> selectUserInfoByCondition(@NotNull UserInfoSelectDto userInfoSelectDto) {
        List<UserInfoVo> userInfoVos = userInfoMapper.getUserInfoListByCondition(userInfoSelectDto);
        return userInfoVos;
    }

    public boolean insertUserAccountBatch(Collection<UserAccount> entityList) {
        return this.saveBatch(entityList);
    }
}
